package com.atguigu.staservice.service.impl;

import com.atguigu.common.R;
import com.atguigu.staservice.client.UcenterClient;
import com.atguigu.staservice.entity.StatisticsDaily;
import com.atguigu.staservice.mapper.StatisticsDailyMapper;
import com.atguigu.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-04-25
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    //1 生成统计数据
    @Override
    public void createSta(String day) {

        //把统计分析表相同日期数据删除
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);

        //远程调用获取某一天注册人数
        R r = ucenterClient.getCountRegister(day);
        //调用返回r对象，从r对象获取注册人数
        Integer countRegister = (Integer)r.getData().get("countRegister");

        //把获取数据添加统计分析表
        StatisticsDaily daily = new StatisticsDaily();
        daily.setRegisterNum(countRegister);//注册人数
        daily.setDateCalculated(day);//统计数据日期
        //生成随机数
        daily.setCourseNum(RandomUtils.nextInt(100,200));
        daily.setLoginNum(RandomUtils.nextInt(100,200));
        daily.setVideoViewNum(RandomUtils.nextInt(100,200));

        baseMapper.insert(daily);
    }

    //图表显示返回统计数据
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        //1 根据条件参数查询数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        //开始和结束时间范围数据
        wrapper.between("date_calculated",begin,end);
        //查询指定字段值
        wrapper.select(type,"date_calculated");
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);

        //按照echarts要求返回数据格式，返回两部分数据
        // 日期： ["2020-04-03","2020-04-09","2020-04-13"]
        // 数量： [3,10,6]
        // 创建两个list集合，一个list放查询日期，一个list集合放数量
        Map<String,Object> map = new HashMap<>();
        List<String> date_calculatedList = new ArrayList<String>();
        List<Integer> dataShowList = new ArrayList<Integer>();

        //遍历查询所有数据staList
        for (int i = 0; i < staList.size(); i++) {
            //得到每个sta对象
            StatisticsDaily sta = staList.get(i);
            //把查询日期放到date_calculatedList里面
            date_calculatedList.add(sta.getDateCalculated());
            //日期对应数量放到dataShowList里面
            //判断查询哪个统计因子
            switch (type) {
                case "login_num": //登录人数
                    dataShowList.add(sta.getLoginNum());
                    break;
                case "register_num": //注册人数
                    dataShowList.add(sta.getRegisterNum());
                    break;
                case "video_view_num": //视频
                    dataShowList.add(sta.getVideoViewNum());
                    break;
                case "course_num": //课程人数
                    dataShowList.add(sta.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        map.put("date_calculatedList",date_calculatedList);
        map.put("dataShowList",dataShowList);
        return map;
    }
}
